import React, {Component} from 'react'
import {Router} from 'react-router';
import axios from 'axios'

/**
 * 封装请求header
 */
// eslint-disable-next-line no-unused-vars
class RequestComponent extends Component {
  constructor(loginId, token, clientType) {
    super(loginId, token, clientType);
    RequestComponent.header = {
      loginId: loginId,
      token: token,
      clientType: clientType
    }
    RequestComponent.promissionAxios.defaults.headers=RequestComponent.header;
  }
  static getAxios=()=>{
  if(!RequestComponent.promissionAxios){
    const header=localStorage.getItem('headers');
    const basicUrl=localStorage.getItem('basicUrl');
    if(!header){
      alert("退出登陆")
      this.context.router.history.push('/login');
      return false;
    }
    RequestComponent.promissionAxios=axios.create({
      baseURL: basicUrl,
      headers:header
    })
  }
    return RequestComponent.promissionAxios;
  }
  static initHost = (environment) => {
    if (environment === 'local') {
      RequestComponent.httpUrl = 'http://guardian.cmiiyl.com'
      // RequestComponent.httpUrl = ''
    }
    if (environment === 'dev') {
      RequestComponent.httpUrl = 'http://guardian.cmiiyl.com'
    }

    if (environment === 'test') {

    }

    RequestComponent.promissionAxios = axios.create({
      baseURL: RequestComponent.httpUrl,
      headers: {}
    })
    //拦截器
    RequestComponent.promissionAxios.interceptors.request.use(function (config) {
      console.log('config',config)
      return config;
    }, function (error) {
      console.log('error',error)
      return Promise.reject(error)
    })
    return RequestComponent.httpUrl
  }

}


export default RequestComponent
